package com.kvest.testtask.android;

import java.util.ArrayList;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.content.Context;
import android.util.Pair;

public class SiteListAdapter extends ArrayAdapter<Pair<Integer, String>>
{
	private ArrayList<Pair<Integer, String>> items;
	private LayoutInflater inflater;
	private int siteViewResourceId;
	private int textViewResourceId;
	private int editViewId;
	private int deleteViewId;
	private OnClickListener onEditClick;
	private OnClickListener onDeleteClick;
	private SiteItemAction siteItemActionListener = null;
	
	
	public SiteListAdapter(Context context, int siteViewResourceId,int textViewResourceId,int editViewId, int deleteViewId, ArrayList<Pair<Integer, String>> items) 
    {
        super(context, siteViewResourceId, items);
        this.inflater = LayoutInflater.from(context);
        this.items = items;
        this.siteViewResourceId = siteViewResourceId;
        this.textViewResourceId = textViewResourceId;
        this.editViewId = editViewId;
        this.deleteViewId = deleteViewId;
        
        createClickListeners();
    }
	
	@Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        View view = convertView;
        if (view == null) 
        {
        	view = this.inflater.inflate(siteViewResourceId, null);
        	view.setOnClickListener(new OnClickListener() 
        	{	
				@Override
				public void onClick(View v) 
				{
					if (siteItemActionListener != null)
					{
						siteItemActionListener.selectAction((Integer)v.getTag());
					}
				}
			});
        }
        
        Pair<Integer, String> element = items.get(position);
        view.setTag(element.first);
        TextView text = (TextView) view.findViewById(textViewResourceId);
        if (element != null && text != null) 
        {
        	text.setText(element.second);
        }
        View editView = view.findViewById(editViewId);
        if (editView != null)
        {
        	editView.setTag(element.first);
        	editView.setOnClickListener(onEditClick);
        }
        View deleteView = view.findViewById(deleteViewId);
        if (deleteView != null)
        {
        	deleteView.setTag(element.first);
        	deleteView.setOnClickListener(onDeleteClick);
        }
        return view;
    }

	public void createClickListeners()
	{
		onEditClick = new OnClickListener() 
		{	
			@Override
			public void onClick(View v) 
			{
				if (siteItemActionListener != null)
				{
					siteItemActionListener.editAction((Integer)v.getTag());
				}
			}
		};
		
		onDeleteClick = new OnClickListener()
		{	
			@Override
			public void onClick(View v) 
			{
				if (siteItemActionListener != null)
				{
					siteItemActionListener.deleteAction((Integer)v.getTag());
				}
			}
		};
	}
	
	public void setSiteItemActionListener(SiteItemAction siteItemActionListener)
	{
		this.siteItemActionListener = siteItemActionListener;
	}
	
	public interface SiteItemAction
	{
		public void selectAction(Integer siteId);
		public void editAction(Integer siteId);
		public void deleteAction(Integer siteId);
	}
}
